<?php
/**
 * [模型]提现
 * 
 * @version $Id: Exchange.php 287 2012-05-02 07:14:49Z linsir123 $
 */
class modelExchange 
{
	/**
	 * @var 数据模型
	 */
	public $dbExchange;
	
	/**
	 * @var 数据模型
	 */
	public $dbAccount;
	
	/**
	 * @var 会员ID
	 */
	public $uId = 0;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$this->dbAccount = D::account();
		$this->dbExchange = D::exchange();
	}
	
	/**
	 * 取得返现金额的相关信息
	 * // 已提现金额, 已提现次数
	 */
	public function getInfo()
	{
		$total = round($this->dbExchange->getPayMoneysByUser($this->uId), 2);
		$count = intval($this->dbExchange->getPayTimesByUser($this->uId));
		
		///
		return compact('total', 'count');
	}
	
	/**
	 * 取得列表
	 */
	public function getList($start, $offset, $params = array())
	{
		$params['uid'] = $this->uId;
		
		///
		return $this->dbExchange->getList($start, $offset, $params);
	}
	
	/**
	 * 取得个数
	 */
	public function getListCount($params = array())
	{
		$params['uid'] = $this->uId;
		
		///
		return $this->dbExchange->getListCount($params);
	}
	
	/**
	 * 创建新的记录
	 * // 验证账户余额
	 * // 验证支付宝是否绑定
	 */
	public function newOne()
	{
		$money = round($_POST['money'], 2);
		$data = M::account()->canApplyPay($money);
		list($flag, $string) = $data;
		if ( ! $flag) 
			return $string;
			
		///
		$alipay = $string;
		$data = array(
			'uid' => $this->uId,
			'money' => $money,
			'alipay' => $alipay,
			'ip' => Request::ip(),
			'apply_time' => time(),
		);
		
		///
		if ($id = $this->dbExchange->insert($data)) {
			$this->dbAccount->updateBy('uid', $this->uId, array('state'=>1));
			return '已成功提交申请';
		}
		return '系统异常，请联系管理员';
	}
}
